package com.finalproject.endice9x9;

import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

//Game Control fragment to change level number, reset game state and display game help info
public class GameControlFragment extends Fragment {
	private ImageView resetImage;
	private ImageView leftArrowImage;
	private ImageView rightArrowImage;
	private ImageView infoImage;
	private GameController controller;

	public GameControlFragment(GameController controller) {
		this.controller = controller;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRetainInstance(true);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_game_control, parent, false);

		resetImage = (ImageView) v.findViewById(R.id.imageViewReset);
		resetImage.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				reset();
			}
		});

		leftArrowImage = (ImageView) v.findViewById(R.id.imageViewLeftArrow);
		rightArrowImage = (ImageView) v.findViewById(R.id.imageViewRightArrow);

		infoImage = (ImageView) v.findViewById(R.id.imageViewInfo);
		infoImage.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				helpInfo();
			}
		});

		return v;
	}

	private void reset() {
		controller.levelReset();
	}

	private void previousLevel() {
		controller.levelDown();
	}

	private void nextLevel() {
		controller.levelUp();
	}

	//Create and show help (info) dialog
	private void helpInfo() {
		FragmentManager fm = getActivity().getFragmentManager();
		InfoFragment dialog = new InfoFragment(R.string.title_insrtuctions,
				R.string.game_instructions);
		dialog.show(fm, InfoFragment.DIALOG_INFO);
	}

	//Enable next level image button 
	public void setNextEnabled(boolean enabled) {
		if (enabled) {
			rightArrowImage.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					nextLevel();
				}
			});
			rightArrowImage.setImageResource(R.drawable.right_arrow);
		} else {
			rightArrowImage.setOnClickListener(null);
			rightArrowImage.setImageResource(R.drawable.right_arrow_disabled);
		}
	}

	//Enable previous level image button
	public void setPrevEnabled(boolean enabled) {
		if (enabled) {
			leftArrowImage.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					previousLevel();
				}
			});
			leftArrowImage.setImageResource(R.drawable.left_arrow);
		} else {
			leftArrowImage.setOnClickListener(null);
			leftArrowImage.setImageResource(R.drawable.left_arrow_disabled);
		}
	}
}
